package main

import (
	"fmt"
	"sync"
)

func printNumbers(wg *sync.WaitGroup, ch chan struct{}, start, end rune, interval rune) {
	defer wg.Done()
	grade := interval
	for i := start; i <= end; i++ {
		fmt.Print(i)
		/*
			   //为什么这两个判断分开写就会死锁？
			   		if i == end {
			   			ch <- struct{}{}
			   			return
			   		}
					if i == grade  {
					grade += interval
					ch <- struct{}{}
					<-ch // 等待接收信号
				}
		*/
		if i == grade || i == end {
			grade += interval
			ch <- struct{}{}
			<-ch // 等待接收信号
		}

	}
}

func printLetters(wg *sync.WaitGroup, ch chan struct{}, start, end rune, interval rune) {
	defer wg.Done()
	for i := start; i <= end; i++ {
		<-ch // 等待接收信号
		for j := i; j < i+interval; j++ {
			fmt.Printf("%c", j+'A'-1)
			if j == end {
				ch <- struct{}{}
				return
			}
		}
		i += interval - 1
		ch <- struct{}{} // 发送信号
	}
}

func main() {
	solution(8, 1, 26)
}
func solution(interval int, start, end rune) {
	val := rune(interval)
	var wg sync.WaitGroup
	wg.Add(2)

	ch := make(chan struct{})

	go printNumbers(&wg, ch, start, end, val)
	go printLetters(&wg, ch, start, end, val)

	wg.Wait()
	close(ch)
}
